#region Header
//=============================================================================
//                          GShield (Game Shield)
//=============================================================================
//
// Original Programmer: Shawn Jackson
// Company: WaveTech Digital Technologies, Inc. (www.wtdt.com)
// Copyright: (c) 2006 WaveTech Digital Technologies, All Rights Reserved.
// Assembly: Engine
// Code File Name: Messaging.cs
// Code File Date: 8/2/2007
//
// Code File Info: Messiging and Notication System
//=============================================================================
/*
 * UPDATES
 * -------
 *
 */
#endregion Header

#region License
/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#endregion License

#region Includes
using System;
using System.Text;
using System.Windows.Forms;
using WaveTech.GShield.Engine.Tools;
#endregion Includes

namespace WaveTech.GShield.Engine.Systems
{
	public static class Messaging
	{
		#region Private Members
		private static int m_CurrentMessageCount = 0;
		#endregion Private Members

		#region Private Constants
		private const MessageBoxIcon m_mbiInfo = MessageBoxIcon.Information;
		private const MessageBoxIcon m_mbiWarning = MessageBoxIcon.Warning;
		private const MessageBoxIcon m_mbiFatal = MessageBoxIcon.Error;
		private const MessageBoxIcon m_mbiQuestion = MessageBoxIcon.Question;
		#endregion Private Constants

		#region Public Properties
		public static string NewLine
		{
			get { return Environment.NewLine; }
		}

		public static string NewParagraph
		{
			get { return Environment.NewLine + Environment.NewLine; }
		}

		public static int CurrentMessageCount
		{
			get { return m_CurrentMessageCount; }
		}
		#endregion Public Properties

		#region Objects To Message
		private static string ObjectsToMessage(object[] vLines)
		{
			return ObjectsToMessage(vLines, false);
		}

		private static string ObjectsToMessage(object[] vLines, bool bFullExceptions)
		{
			if (vLines == null) return string.Empty;

			string strNewPara = NewParagraph;

			StringBuilder sbText = new StringBuilder();
			bool bSeparator = false;

			foreach (object obj in vLines)
			{
				if (obj == null) continue;

				string strAppend = null;

				Exception exObj = obj as Exception;
				string strObj = obj as string;

				if (exObj != null)
				{
					if (bFullExceptions)
						strAppend = StringTools.FormatException(exObj);
					else if ((exObj.Message != null) && (exObj.Message.Length > 0))
						strAppend = exObj.Message;
				}
				else if (strObj != null)
					strAppend = strObj;
				else
					strAppend = obj.ToString();

				if ((strAppend != null) && (strAppend.Length > 0))
				{
					if (bSeparator) sbText.Append(strNewPara);
					else bSeparator = true;

					sbText.Append(strAppend);
				}
			}

			return sbText.ToString();
		}
		#endregion Objects To Message

		#region Show Usage Warning
		/// <summary>
		/// Shows a standard Usage Warning dialog box
		/// </summary>
		/// <param name="time">Total time spent playing</param>
		/// <param name="limit">Time limit imposed by the policy</param>
		public static void ShowUsageWarning(int time, int limit)
		{
			m_CurrentMessageCount++;

			MessageBox.Show("The total playing time for today, " + time + " minutes, has exceeded the limit, " +
											limit + " minutes.", "GShield",
				MessageBoxButtons.OK, m_mbiWarning, MessageBoxDefaultButton.Button1);

			m_CurrentMessageCount--;
		}
		#endregion Show Usage Warning

		#region Show Ratings Warning
		/// <summary>
		/// Shows a standard dialog box for a ESRB game rating violation
		/// </summary>
		public static void ShowRatingsWarning()
		{
			m_CurrentMessageCount++;

			MessageBox.Show("This game has violated the ratings policy and has been terminated.", "GShield",
				MessageBoxButtons.OK, m_mbiWarning, MessageBoxDefaultButton.Button1);

			m_CurrentMessageCount--;
		}
		#endregion Show Ratings Warning

		#region Show Banned Warning
		/// <summary>
		/// Shows a standard dialog box for a banned game policy violation
		/// </summary>
		public static void ShowBannedWarning()
		{
			m_CurrentMessageCount++;

			MessageBox.Show("This game has violated the banned games policy and has been terminated.", "GShield",
				MessageBoxButtons.OK, m_mbiWarning, MessageBoxDefaultButton.Button1);

			m_CurrentMessageCount--;
		}
		#endregion Show Banned Warning

		public static void ShowInfo(params object[] lines)
		{
			string strText = ObjectsToMessage(lines);

			m_CurrentMessageCount++;

			MessageBox.Show(strText, "GShield",
				MessageBoxButtons.OK, m_mbiInfo, MessageBoxDefaultButton.Button1);

			m_CurrentMessageCount--;
		}

		public static void ShowWarning(params object[] lines)
		{
			string strText = ObjectsToMessage(lines);

			m_CurrentMessageCount++;

			MessageBox.Show(strText, "GShield",
				MessageBoxButtons.OK, m_mbiWarning, MessageBoxDefaultButton.Button1);

			m_CurrentMessageCount--;
		}

		public static void ShowFatal(params object[] lines)
		{
			string strText = "A fatal error has occured!" + NewParagraph +
				ObjectsToMessage(lines);

			try
			{
				Clipboard.Clear();
				Clipboard.SetText(ObjectsToMessage(lines, true));
			}
			catch { }

			m_CurrentMessageCount++;

			MessageBox.Show(strText, "GShield" + @" - " +
				"A fatal error has occured!", MessageBoxButtons.OK, m_mbiFatal,
				MessageBoxDefaultButton.Button1);

			m_CurrentMessageCount--;
		}

		public static DialogResult Ask(string text, string title, MessageBoxButtons buttons)
		{
			string strTextEx = ((text != null) ? text : string.Empty);
			string strTitleEx = ((title != null) ? title : "GShield");

			m_CurrentMessageCount++;

			DialogResult dr = MessageBox.Show(strTextEx, strTitleEx, buttons,
				m_mbiQuestion, MessageBoxDefaultButton.Button1);

			m_CurrentMessageCount--;

			return dr;
		}

		public static bool AskYesNo(string text, string title)
		{
			string strTextEx = ((text != null) ? text : string.Empty);
			string strTitleEx = ((title != null) ? title : "GShield");

			m_CurrentMessageCount++;

			DialogResult dr = MessageBox.Show(strTextEx, strTitleEx, MessageBoxButtons.YesNo,
				m_mbiQuestion, MessageBoxDefaultButton.Button1);

			m_CurrentMessageCount--;

			return (dr == DialogResult.Yes);
		}

		public static bool AskYesNo(string text)
		{
			return AskYesNo(text, null);
		}
	}
}